package vlms.controller;

import java.io.IOException;
import java.rmi.RemoteException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.Account;
import vlms.bean.Person;
import vlms.service.VLMSServiceProxy;

/**
 * Servlet implementation class Profile
 */
public class Profile extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Profile() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String destination = "/home.jsp";
		HttpSession session = request.getSession();
		Person oPerson = (Person) session.getAttribute("Person");
		Account account =  (Account) session.getAttribute("Account");
		
		oPerson.setFirstname(request.getParameter("firstname"));
		oPerson.setLastname(request.getParameter("lastname"));
		oPerson.setAddress(request.getParameter("address"));
		oPerson.setEmailid(request.getParameter("email"));
		oPerson.setCity(request.getParameter("city"));
		oPerson.setState(request.getParameterValues("state")[0]); // Change
		oPerson.setZipcode(request.getParameter("zipcode"));
		oPerson.setCreditcardcvv(Integer.parseInt(request.getParameter("Cvvnumber")));
		oPerson.setCreditcardnum(request.getParameter("creditcardno"));
		oPerson.setCreditcardtype(request.getParameterValues("creditcard_type")[0]); // Change
		account.setUsername(request.getParameter("username"));
		try
		 {
			 VLMSServiceProxy proxy = new VLMSServiceProxy();
			 proxy.updateMember(account, oPerson);
			 request.setAttribute("message", "Your Profile is successfully updated.");
		 } 	
		 catch(RemoteException re)
		 {
			 request.setAttribute("message", re.getMessage());
			 re.printStackTrace();
		 }
		
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
		rd.forward(request, response);
	}

}
